//
//  MyCollectionViewController.m
//  TravelEveryDay
//
//  Created by 路洪宇 on 15/10/21.
//  Copyright © 2015年 com.lanou3g. All rights reserved.
//

#import "MyCollectionViewController.h"
#import "DataBase.h"
#import "MyTableViewCell.h"
#import "PlayDetailViewController.h"
@interface MyCollectionViewController ()<UITableViewDataSource,UITableViewDelegate>

@end

@implementation MyCollectionViewController
-(void)viewWillAppear:(BOOL)animated
{
    _dataArray =[[NSMutableArray alloc]init];
    [self setTabelView];
    
    _dataArray = [[DataBase shareDataBase]selectAll];
    NSLog(@"%@",_dataArray);
    [_attentionTabelView reloadData];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithRed:0.98 green:0.97 blue:0.93 alpha:1];
    [self setUpTitleView];
    [self setTabelView];
}

-(void)setUpTitleView
{
    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64)];
    [titleView setBackgroundColor:TITLECOLOR];
    [self.view addSubview:titleView];
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [backButton setImage:[UIImage imageNamed:@"play-jiantouzuo.png"] forState:UIControlStateNormal];
    [backButton setFrame:CGRectMake(10, 30, 22, 22)];
    [backButton addTarget:self action:@selector(leftClick) forControlEvents:UIControlEventTouchUpInside];
    [titleView addSubview:backButton];
    
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/2-100, 25, 200, 30)];
    [title setText:@"喜欢"];
    [title setTextColor:[UIColor whiteColor]];
    title.font = [UIFont systemFontOfSize:19.];
    title.textAlignment = NSTextAlignmentCenter;
    [titleView addSubview:title];
    
    
    [self.view addSubview:titleView];
}

-(void)leftClick
{
#pragma mark- 返回指定页面
    UIViewController *vc =[self.navigationController.viewControllers objectAtIndex:0];
    [self.navigationController popToViewController:vc animated:YES];
    self.tabBarController.tabBar.hidden = NO;
    
}


#pragma mark- setTabelView
-(void)setTabelView
{
    _attentionTabelView =[[UITableView alloc]initWithFrame:CGRectMake(-1, 64, SCREEN_WIDTH, SCREEN_HEIGHT-90)];
    _attentionTabelView.delegate = self;
    _attentionTabelView.dataSource = self;
    [_attentionTabelView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    _attentionTabelView.backgroundColor = [UIColor colorWithRed:0.98 green:0.97 blue:0.93 alpha:1];
    [self.view addSubview:_attentionTabelView];

#pragma mark-添加手势
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeClick :)];
    
    [swipe setDirection:UISwipeGestureRecognizerDirectionRight];
    
    [_attentionTabelView addGestureRecognizer:swipe];
}
#pragma mark-滑动手势
-(void)swipeClick:(UISwipeGestureRecognizer *)swipe
{
    UIViewController *vc =[self.navigationController.viewControllers objectAtIndex:0];
    [self.navigationController popToViewController:vc animated:YES];
}
#pragma mark- tabelView delegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return  _dataArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 140;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    _model =[_dataArray objectAtIndex:indexPath.row];
    static NSString *cellId = @"MyTableViewCell";
    MyTableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        cell = [[MyTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    [cell getInfo:_model];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    PlayDetailViewController *push = [[PlayDetailViewController alloc]init];
    push.tempModel = [_dataArray objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:push animated:YES];
}




#pragma mark- 滑动删除
//需要三个方法
//1.可以编辑
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
//2.编辑状态
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}
-(void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    //设置tableview的编辑状态 让编辑按钮的状态与tableview的编辑状态同步
    [self.attentionTabelView setEditing:editing animated:animated];
    
}
//3.
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        _model =[_dataArray objectAtIndex:indexPath.row];
        [[DataBase shareDataBase]deleteStudent:_model.title];
        [_dataArray removeObject:_model];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    }
}

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    PlayModel *model = [_dataArray objectAtIndex:sourceIndexPath.row];
    [_dataArray removeObjectAtIndex:sourceIndexPath.row];
    [_dataArray insertObject:model atIndex:destinationIndexPath.row];
    
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
    
}




#pragma mark 空模板下的图片和文字
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
{
    return [UIImage imageNamed:[NSString stringWithFormat:@"bg_guider_2_736h@2x.jpg"]];
}
- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
{
    NSString *text = @"This allows you to search car.";
    NSDictionary *attributes = @{NSFontAttributeName: [UIFont boldSystemFontOfSize:16.0f],
                                 NSForegroundColorAttributeName: [UIColor darkGrayColor]};
    return [[NSAttributedString alloc] initWithString:text attributes:attributes];
}
- (BOOL)emptyDataSetShouldDisplay:(UIScrollView *)scrollView
{
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
